package com.example.cinemaorder.service.impl;

import com.example.cinemaorder.entity.vo.SearchMovieInfoVo;
import com.example.cinemaorder.entity.vo.SearchMovieVo;
import com.example.cinemaorder.entity.vo.SysRatingVo;
import com.example.cinemaorder.mapper.SysMovieMapper;
import com.example.cinemaorder.service.SysMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysMovieServiceImpl implements SysMovieService {

    @Autowired
    private SysMovieMapper sysMovieMapper;
    @Autowired
    RedisTemplate redisTemplate;

//  电影详情界面，根据id查询电影信息
    @Override
    public Object findMovieById(Long id) {
        return sysMovieMapper.findMovieById(id);
    }

//   电影排片界面，搜索
    public List<SearchMovieVo> search(SearchMovieInfoVo searchMovieInfoVo) {
        //添加年份信息
        int year = LocalDateTime.now().getYear();
        //当值为-1代表是选中全部
        if(searchMovieInfoVo.getTime().equals("-1") == false){
            searchMovieInfoVo.setTime(year+"-"+searchMovieInfoVo.getTime().substring(0,2)+"-"+
                    searchMovieInfoVo.getTime().substring(3,5));
        }
        return sysMovieMapper.search(searchMovieInfoVo);
    }
//  电影排片界面，列出所有的影厅
    public List<String> getHalls() {
        return sysMovieMapper.getHalls();
    }

//    电影排片界面，列出所有的影院
    public Object getCinemas() {
        return sysMovieMapper.getCinemas();
    }

    @Override
    public Object getIds() {
        return sysMovieMapper.getIds();
    }

    @Override
    public Object getRating(Long movieId,Long userId) {
        Map entries = redisTemplate.opsForHash().entries("电影评分：" + movieId.toString());
        return entries.values().toArray()[entries.values().toArray().length-1];
    }

    @Override
    public Object getRatingByBill(Long billId) {
        return sysMovieMapper.getRatingByBill(billId);
    }

    @Override
    public Object sendRating(SysRatingVo sysRatingVo) {
        //判断是否已经提交
        SysRatingVo o = sysMovieMapper.checkSend(sysRatingVo);
        if(o != null){
            return false;
        }
        // 重新计算评分
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        Map entries = redisTemplate.opsForHash().entries("电影评分：" + sysRatingVo.getMovieId().toString());
        double rating;
        rating = ((Double.parseDouble(entries.values().toArray()[0].toString())+sysRatingVo.getRating())/(Double.parseDouble(entries.keySet().toArray()[0].toString())*(-1)+1));
        // 存储在redis中
        redisTemplate.opsForHash().put("电影评分："+sysRatingVo.getMovieId().toString(),(-1)*(Double.parseDouble(entries.keySet().toArray()[0].toString())*(-1)+1)+"",rating+"");
        sysMovieMapper.sendRating(sysRatingVo);
        return true;
    }
}
